package top.haijunit.springboot.bucket.springsecurity02.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import top.haijunit.springboot.bucket.springsecurity02.entity.UserEntity;
import top.haijunit.springboot.bucket.springsecurity02.mapper.UserMapper;

/**
 * @Author 百旺张
 * @Date 2021/11/9
 * @Description
 */
@Service
public class UserService extends ServiceImpl<UserMapper, UserEntity> implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StrUtil.isBlank(username)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEntity::getUsername, username);
        UserEntity user = this.baseMapper.selectOne(wrapper);
        if(null == user){
            throw new UsernameNotFoundException("用户不存在");
        }
        return user;
    }
}
